<?php
/** 
 * 送能量
 * Author:zhang
 *
 */

namespace Api\Controller;

class EnergyController extends ApiController{
	public function __construct(){
    	parent::__construct();
    	/** 测试不判断登录 modify by zhang 2016 start  **/
    	if (!I('mytest')) {
	        !api_login() && response(-94);
	        !passed_login() && response(-95);
	        !only_login() && response(-96);
    	}
    	/** 测试不判断登录 modify by zhang 2016 end **/
	}
    //发说说
    function add(){
	    $content = I('post.content');
        $data = array(
            'uid' => API_UID,
            'content' => $content,
            'cover_ids' => I('post.cover_ids'),
            'movie_id' => I('post.movie_id'),
            'create_time' => NOW_TIME,
            'is_energy' => I('post.is_energy', 0)//0代表送能量，1代表分享屋
        );
        if (!$content) response(-2,'内容不能为空');
        $result = M('energy')->add($data);
        $result && response(1, $result) && response(-9);
    }
    //点赞
    function zan(){
        params_exist(array('e_id'));
        $e_id = I('e_id');
        //是否已点赞
        $check = M('energy_praise')->where(array('e_id'=>$e_id,'z_id'=>API_UID))->count();
        if ($check > 0) {
            response(-2,"您已赞过");
        }
        $data = array(
            'e_id' => $e_id,
            'z_id' => API_UID,
            'z_time' =>NOW_TIME
        );
        $result = M('energy_praise')->add($data);
        $result && response(1) && response(-9,'点赞出错');
    }
    //添加评论
    function addComment(){
        params_exist(array('e_id', 'text'));
        $data = M('energy_comment')->create();
        $data['z_id'] = API_UID;
        $data['z_time'] = NOW_TIME;
        if (M('energy_comment')->add($data)) {
            response(1);
        }else{
            response(-9,'操作失败');
        }
    }
    //删除评论
    function deleteComment(){
        params_exist(array('id'));
        $id = intval(I('id'));
        if (M('energy_comment')->delete($id)) {
            response(1);
        }else{
            response(-9);
        }
    }
    //删除说说
    function delete(){
        params_exist(array('id'));
        $id = intval(I('id'));
        if (M('energy')->delete($id)) {
            M('energy_comment')->where(array('e_id'=>$id))->delete();
            M('energy_praise')->where(array('e_id'=>$id))->delete();
            response(1);
        }else{
            response(-9);
        }
    }
    //获取说说详情（评论）
    public function getCommentDetail() {
        $eid = I('post.eid');
        $data['energy'] = M('energy')->where('id='.$eid)->find();
        $member = M('member')->where('uid='.$data['energy']['uid'])->find();
        $data['energy']['headimg'] = M('picture')->where('id='.$member['avatar'])->getField('path');//发布说说人的头像
        $data['energy']['username'] = $member['nickname'];//发布说说人的昵称
        $getComment = M('energy_comment')->where('e_id='.$eid)->select();
        $data['energy']['times'] = M('energy')->where('id='.$eid)->save(array('times'=>$data['times']+1));//阅读量加1
        foreach($getComment as $key=>$val) {
            $memberCom = M('member')->where('uid='.$val['z_id'])->find();
            $getComment[$key]['headimg'] = M('picture')->where('id='.$memberCom['avatar'])->getField('path');//发布评论人的头像
            $getComment[$key]['username'] = $memberCom['nickname'];//发布评论人的昵称
        }
        $data['energy']['comment'] = $getComment;
        $data && response(1, $data) && response(-9);
    }
    //投诉
    public function complaint() {
        $data = array(
            'content' => I('post.content'),
            'uid' => API_UID,//谁投诉的
            'eid' => I('post.eid'),
            'create_time' => NOW_TIME
        );
        $result = M('energy_complaint')->add($data);
        $result && response(1, $result) && response(-9);
    }
    //获取圈子动态  Todo
    function energyList(){
        $uid = API_UID;
        $is_energy = I('is_energy');
        $page = I('page', 1);
        $pagesize = I('pagesize', 10);
        $offset = ($page-1) * $pagesize;
        $userids = M('user_friend')->field('fid')->where('uid='.$uid)->select();
        $res = $this->arrayToString($userids, 'fid');
        $where['e.uid'] = array('in',$uid.','.$res);
        $where['is_energy'] = array('eq', $is_energy);
        $list = M('energy')->alias('e')
                ->field('e.*,u.nickname,u.avatar')
                ->join('left join __MEMBER__ u on e.uid=u.uid')
                ->where($where)
                ->limit($offset, $pagesize)
                ->order('create_time desc')
                ->select();
        if ($list) {
            if ($is_energy == 0) {
                foreach ($list as $key => $value) {
                    //获取人物头像
                    $list[$key]['headimg'] = M('picture')->where('id='.$value['avatar'])->getField('path');
                    //说说图片和视频地址
                    $cover_ids= explode(',', $value['cover_ids']);
                    foreach ($cover_ids as $key1=>$value1) {
                        $list[$key]['imgPath'][$key1] = M('picture')->where('id='.$value1)->getField('path');
                    }
                    $list[$key]['moviePath'] = M('movie')->where('id='.$value['movie_id'])->getField('savepath');
                    $list[$key]['create_time'] = transfer_time($value['create_time']);
                    //获取评论数
                    $list[$key]['countComment'] = M('energy_comment')->where('e_id='.$value['id'])->count();
                }
            } else {
                foreach ($list as $key => $value) {
                    //获取人物头像
                    $list[$key]['headimg'] = M('picture')->where('id='.$value['avatar'])->getField('path');
                    //说说图片和视频地址
                    $cover_ids= explode(',', $value['cover_ids']);
                    foreach ($cover_ids as $key1=>$value1) {
                        $list[$key]['imgPath'][$key1] = M('picture')->where('id='.$value1)->getField('path');
                    }
                    $list[$key]['moviePath'] = M('movie')->where('id='.$value['movie_id'])->getField('savepath');
                    $list[$key]['create_time'] = transfer_time($value['create_time']);
                    //获取评论数
                    $list[$key]['countComment'] = M('energy_comment')->where('e_id='.$value['id'])->count();
                    //获取点赞名单
                    $praiseArr = M('energy_praise')->alias('p')
                        ->field('u.nickname')
                        ->join('left join __MEMBER__ u on p.z_id=u.uid')
                        ->where('p.e_id='.$value['id'])->order('p.z_time asc')->select();
                    $list[$key]['praise'] = $this->arrayToString($praiseArr, 'nickname');
                    //获取评论
                    $list[$key]['comment'] = M('energy_comment')->where('e_id='.$value['id'])->limit(2)->order('z_time desc')->select();
                    //获取评论用户头像
                    foreach ($list[$key]['comment'] as $key2=>$value2) {
                        $list[$key]['comment'][$key2]['nickname'] = M('member')->where('uid='.$value2['z_id'])->getField('nickname');
                        $list[$key]['comment'][$key2]['headimg'] = M('picture')->where('id='.$value2['avatar'])->getField('path');
                    }
                }
            }
            response(1,$list);
        } else {
            response(-9);
        }
    }
    public function arrayToString($array, $val, $plite=',') {
        $res = array();
        foreach ($array as $key=>$value) {
            $res[] = $value[$val];
        }
        $res = implode($plite, $res);
        return $res;
    }
}